const sequelize = require("../db");
const {DataTypes} = require("sequelize");
const {checkCookieIsExpire, parseCookies} = require("../../utils/fun");

const User = sequelize.define("user", {
    id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true
    },
    username: DataTypes.TEXT,
    password: DataTypes.TEXT,
    game_id:DataTypes.TEXT,
    anchor_id:DataTypes.TEXT,
    wx_id:DataTypes.INTEGER,//微信昵称
    login_info: DataTypes.TEXT, //登录信息
    role_info:DataTypes.TEXT, //角色信息
    cookies: DataTypes.TEXT,
    device:DataTypes.TEXT, //设置信息
    client_ip: DataTypes.TEXT, //伪造的客户端ip
    bind_mobile:DataTypes.TEXT,
    registered_at:DataTypes.DATE,
    recharge_money: {
        type:DataTypes.FLOAT(10,2),
        defaultValue: 188
    },
    total_money: {
        type:DataTypes.FLOAT(10,2),
        defaultValue: 0
    },
    balance: {
        type:DataTypes.FLOAT(10,2),
        defaultValue: 0
    },
    remark:DataTypes.TEXT,

    //虚拟字段
    cookie_is_valid: {
        type: DataTypes.VIRTUAL,
        get() {
            if(!this.cookies){
                return false
            }
            return !checkCookieIsExpire(this.cookies);

        }
    }
    ,cookies_obj: {
        type: DataTypes.VIRTUAL,
        get() {
            return parseCookies(this.cookies)
        }
    }
    ,role_fmt_info: {
        type: DataTypes.VIRTUAL,
        get(){
            return JSON.parse(this.role_info)
        }
    }
    ,login_fmt_info:{
        type: DataTypes.VIRTUAL,
        get(){
            return JSON.parse(this.login_info)
        }
    }
},{
    deletedAt: "deleted_at",
    paranoid: true,
});



module.exports = User